const { Op } = require("sequelize");

const contentImageModel = appRequire('models/contentImage');

// 导出对象，供其它模块调用
module.exports = {
    list,
    save,
    destroy,
    addClickNum,
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    let params = {}
    let newResults = []
    let contentImage = await contentImageModel.findAll({
        where: params,
        order: [['groupNum', 'ASC'], ['createdAt', 'DESC']],
        //limit: postData.pageSize,
        //offset: postData.pageSize * (postData.pageNum - 1)
    })
    contentImage = Common.clone(contentImage)

    for (let item of contentImage) {
        let thisNewItem = {
            classDesc: item.classDesc,
            results: []
        }
        let thisIndex = newResults.findIndex(newResult => newResult.classDesc == thisNewItem.classDesc)
        if (thisIndex == -1) {
            newResults.push(thisNewItem)
        }
    }

    for (let item of contentImage) {
        let thisIndex = newResults.findIndex(newResult => newResult.classDesc == item.classDesc)
        if (thisIndex != -1) {
            newResults[thisIndex].results.push(item)
        }
    }


    returnData.data = newResults
    request.json(returnData)
}

async function save(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [ 'content'], request);
    if (checkResult == false) return;

    let params = {
        title: postData.title,
        content: postData.content,
        desc: postData.desc,
        groupNum: postData.groupNum,
        images: postData.images,
    }
    if (postData.classDesc) params.classDesc = postData.classDesc
    if (postData.objectId) {
        let result = await contentImageModel.update(params, {
            where: {
                objectId: postData.objectId
            }
        })
        returnData.data = result
    } else {
        params.objectId = sid.uuid()
        let result = await contentImageModel.create(params)
        returnData.data = result
    }

    request.json(returnData)
}

async function destroy(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['objectId'], request);
    if (checkResult == false) return;

    let result = await contentImageModel.destroy({
        where: {
            objectId: postData.objectId
        }
    })
    returnData.data = result
    request.json(returnData)
}

async function addClickNum(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, ['objectId'], request);
    if (checkResult == false) return;
    let thiscontentImage = await contentImageModel.findOne({
        where: {
            objectId: postData.objectId
        }
    })
    if (thiscontentImage) {
        await contentImageModel.update({
            clickNum: thiscontentImage.clickNum + 1
        }, {
            where: {
                objectId: thiscontentImage.objectId
            }
        })
    }

    returnData.data = thiscontentImage
    request.json(returnData)
}